package cn.anecansaitin.whimsy.generator.data.loot.modifier;

import cn.anecansaitin.whimsy.common.loot.SimpleLootModifier;
import cn.anecansaitin.whimsy.common.loot.predicate.LootPathCheck;
import cn.anecansaitin.whimsy.common.register.ModEnchantments;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.entries.EmptyLootItem;
import net.minecraft.world.level.storage.loot.entries.EntryGroup;
import net.minecraft.world.level.storage.loot.entries.LootItem;
import net.minecraft.world.level.storage.loot.functions.EnchantWithLevelsFunction;
import net.minecraft.world.level.storage.loot.functions.SetEnchantmentsFunction;
import net.minecraft.world.level.storage.loot.functions.SetItemDamageFunction;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
import net.minecraft.world.level.storage.loot.providers.number.ConstantValue;
import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
import net.neoforged.neoforge.common.data.GlobalLootModifierProvider;

import java.util.List;

public class ModLootModifierProvider extends GlobalLootModifierProvider {
    public ModLootModifierProvider(PackOutput output, String modId) {
        super(output, modId);
    }

    @Override
    protected void start() {
        //远古城箱子
        add("chests/ancient_city", new SimpleLootModifier(new LootItemCondition[]{LootPathCheck.lootPath(new ResourceLocation("chests/ancient_city")).build()}, List.of(
                LootPool.lootPool()
                        .setRolls(ConstantValue.exactly(1f))
                        .add(EntryGroup.list(
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(2)
                                        .apply(new SetEnchantmentsFunction.Builder()
                                                .withEnchantment(ModEnchantments.MANUAL_MOTOR_COORDINATION.get(), ConstantValue.exactly(1f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(4)
                                        .apply(new SetEnchantmentsFunction.Builder()
                                                .withEnchantment(ModEnchantments.ARROW_REST.get(), ConstantValue.exactly(1f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(3)
                                        .apply(new SetEnchantmentsFunction.Builder()
                                                .withEnchantment(ModEnchantments.RAPID_FIRE.get(), ConstantValue.exactly(1f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .apply(new SetEnchantmentsFunction.Builder()
                                                .withEnchantment(ModEnchantments.AUTOMATIC_TRIGGER.get(), ConstantValue.exactly(1f)))))
                        .add(EmptyLootItem.emptyItem()
                                .setWeight(9))
                        .build())));
        //林地府邸箱子
        add("chests/woodland_mansion", new SimpleLootModifier(new LootItemCondition[]{LootPathCheck.lootPath(new ResourceLocation("chests/woodland_mansion")).build()}, List.of(
                LootPool.lootPool()
                        .setRolls(ConstantValue.exactly(1f))
                        .add(LootItem
                                .lootTableItem(Items.BOW)
                                .setWeight(2)
                                .apply(new EnchantWithLevelsFunction.Builder(UniformGenerator.between(1, 3)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.ARROW_REST.get(), UniformGenerator.between(1, 2))
                                        .when(LootItemRandomChanceCondition.randomChance(0.2f)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.RAPID_FIRE.get(), UniformGenerator.between(1, 4))
                                        .when(LootItemRandomChanceCondition.randomChance(0.2f)))
                                .apply(SetItemDamageFunction.setDamage(UniformGenerator.between(0.3f, 0.95f))))
                        .add(EntryGroup.list(
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(2)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.MANUAL_MOTOR_COORDINATION.get(), ConstantValue.exactly(1f)))
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1))
                                                .when(LootItemRandomChanceCondition.randomChance(0.5f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(4)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.ARROW_REST.get(), ConstantValue.exactly(1f)))
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1))
                                                .when(LootItemRandomChanceCondition.randomChance(0.5f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(3)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.RAPID_FIRE.get(), ConstantValue.exactly(1f)))
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1))
                                                .when(LootItemRandomChanceCondition.randomChance(0.5f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.AUTOMATIC_TRIGGER.get(), ConstantValue.exactly(1f)))
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1))
                                                .when(LootItemRandomChanceCondition.randomChance(0.5f)))))
                        .add(EmptyLootItem.emptyItem()
                                .setWeight(5))
                        .build())));
        //末地城
        add("chests/end_city_treasure", new SimpleLootModifier(new LootItemCondition[]{LootPathCheck.lootPath(new ResourceLocation("chests/end_city_treasure")).build()}, List.of(
                LootPool.lootPool()
                        .setRolls(UniformGenerator.between(1f, 3f))
                        .apply(new SetEnchantmentsFunction.Builder(true)
                                .withEnchantment(Enchantments.VANISHING_CURSE, ConstantValue.exactly(1f))
                                .when(LootItemRandomChanceCondition.randomChance(0.2f)))
                        .add(EntryGroup.list(
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(2)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.MANUAL_MOTOR_COORDINATION.get(), ConstantValue.exactly(1f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(4)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.ARROW_REST.get(), ConstantValue.exactly(1f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .setWeight(3)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.RAPID_FIRE.get(), UniformGenerator.between(1f, 4f))),
                                LootItem.lootTableItem(Items.BOOK)
                                        .apply(new SetEnchantmentsFunction.Builder(true)
                                                .withEnchantment(ModEnchantments.AUTOMATIC_TRIGGER.get(), ConstantValue.exactly(1f)))))
                        .add(LootItem.lootTableItem(Items.BOW)
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.MANUAL_MOTOR_COORDINATION.get(), ConstantValue.exactly(1f))
                                        .when(LootItemRandomChanceCondition.randomChance(0.2f)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.ARROW_REST.get(), UniformGenerator.between(1f, 3f))
                                        .when(LootItemRandomChanceCondition.randomChance(0.5f)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.RAPID_FIRE.get(), UniformGenerator.between(2f, 8f))
                                        .when(LootItemRandomChanceCondition.randomChance(0.4f)))
                                .apply(new SetEnchantmentsFunction.Builder(true)
                                        .withEnchantment(ModEnchantments.AUTOMATIC_TRIGGER.get(), ConstantValue.exactly(1f))
                                        .when(LootItemRandomChanceCondition.randomChance(0.1f)))
                                .when(LootItemRandomChanceCondition.randomChance(0.2f)))
                        .add(EmptyLootItem.emptyItem())
                        .build())));
    }
}
